package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxCartService;
import org.linlinjava.litemall.wx.vo.CartAddRequest;
import org.linlinjava.litemall.wx.vo.CartCheckOutRequest;
import org.linlinjava.litemall.wx.vo.CartUpdateRequest;
import org.linlinjava.litemall.wx.vo.cart.CartAddReqVO;
import org.linlinjava.litemall.wx.vo.cart.CartAvailableCartCouponsReqVO;
import org.linlinjava.litemall.wx.vo.cart.CartRushToPlaceOrderReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(value="购物车controller",tags={"购物车操作接口"})
@RestController
@RequestMapping("/wx/cart")
@Validated
public class WxCartController {


    @Autowired
    private WxCartService wxCartService;


    @ApiOperation(value="用户购物车信息")
    @GetMapping("list")
    @LogAnno
    public Response list(@LoginUser Integer userId,Integer shopId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.list(userId,shopId);
    }

    @ApiOperation(value="添加商品")
    @PostMapping("add")
    @LogAnno
    public Response add(@LoginUser Integer userId, @Valid @RequestBody CartAddReqVO reqVO) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.addCart(userId, reqVO);
    }

    @ApiOperation(value="购物车下单")
    @PostMapping("checkout")
    @LogAnno
    public Response checkout(@ApiIgnore @LoginUser Integer userId, @Valid @RequestBody CartCheckOutRequest cartCheckOutRequest) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.checkOut(userId, cartCheckOutRequest);
    }


    @ApiOperation(value="商品立即下单")
    @PostMapping("rushToPlaceOrder")
    @LogAnno
    public Response rushToPlaceOrder(@LoginUser Integer userId, @Valid @RequestBody CartRushToPlaceOrderReqVO reqVO) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.rushToPlaceOrder(userId, reqVO);
    }

    @ApiOperation(value="删除商品")
    @PutMapping("delete")
    @LogAnno
    public Response delete(@LoginUser Integer userId, @NotNull @RequestParam("ids") List<Integer> ids) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.delete(userId,ids);
    }

    @ApiOperation(value="加减商品数量")
    @PutMapping("update-num")
    @LogAnno
    public Response updateNum(@LoginUser Integer userId, @NotNull @RequestParam("id") Integer id, @NotNull @RequestParam("num") Integer num) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.updateNum(userId,id,num);
    }




    @ApiOperation(value="购物车可用优惠券")
    @PostMapping("available-coupons")
    @LogAnno
    public Response getAvailableCartCoupons(@ApiIgnore @LoginUser Integer userId,@RequestBody CartAvailableCartCouponsReqVO reqVO) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.getAvailableCartCoupons(reqVO);
    }


    /*@ApiOperation(value="用户购物车信息")
    @GetMapping("index")
    @LogAnno
    public Response index(@LoginUser Integer userId,Integer shopId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.index(userId,shopId);
    }*/

    /**
     * 保存编辑购物车
     *
     * @param userId  用户id
     * @param request 请求
     * @return {@link Object}
     */
    @ApiOperation(value="保存编辑购物车")
    @PostMapping("saveEditCart")
    @LogAnno
    public Object saveEditCart(@ApiIgnore @LoginUser Integer userId,@Valid @RequestBody CartAddRequest request){
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxCartService.saveEditCart(request);
    }





    /**
     * 更新
     * 修改购物车商品货品数量
     *
     * @param userId  用户ID
     * @param request 请求
     * @return 修改结果
     */
    @ApiOperation(value="修改商品数量")
    @PostMapping("update")
    @LogAnno
    public Object update(@LoginUser Integer userId, @Valid @RequestBody CartUpdateRequest request) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        return wxCartService.update(userId, request);
    }


    /*@PostMapping("checked")
    @LogAnno
    public Response checked(@LoginUser Integer userId, @RequestBody String body) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        return wxCartService.checked(userId, body);
    }*/



    /**
     * goodscount
     * 购物车商品货品数量
     * <p>
     * 如果用户没有登录，则返回空数据。
     *
     * @param userId 用户ID
     * @return 购物车商品货品数量
     */
    @GetMapping("goodscount")
    @LogAnno
    public Object goodscount(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.ok(0);
        }
        return wxCartService.goodscount(userId);
    }






    /**
     * 切换门店，修改购物车信息
     *
     * @param userId 用户id
     * @param shopId 商店id
     * @return {@link Object}
     */
    @GetMapping("changeShop")
    @LogAnno
    public Object changeShop(@LoginUser Integer userId, @NotNull Integer shopId) {
        if(userId == null){
            return ResponseUtil.unlogin();
        }
        return wxCartService.changeShop(userId, shopId);
    }
}
